package com.hanlin.beforejavacodesummary.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanlin.beforejavacodesummary.common.Result;
import com.hanlin.beforejavacodesummary.mapper.AccountMapper;
import com.hanlin.beforejavacodesummary.pojo.po.AccountDO;
import com.hanlin.beforejavacodesummary.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: lijing
 * @since: 2025-09-29
 * @description:
 */
@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @PostMapping("/insert")
    public Result insert(@RequestBody AccountDO accountDO){

        AccountDO accountDb = accountService.getOne(new LambdaQueryWrapper<AccountDO>().eq(AccountDO::getAccountNumber, accountDO.getAccountNumber()));
        if(accountDb!=null){
            return Result.success("已存在同 AccountNumber 数据");
        }
        boolean flag = accountService.insertData(accountDO);
        if(flag){
            return Result.success("新增数据成功");
        }else {
            return Result.error();
        }
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestParam Long id){
        boolean flag = accountService.removeById(id);
        if(flag){
            return Result.success("删除数据成功");
        }else {
            return Result.error();
        }
    }




    @PostMapping("update")
    public Result update(@RequestBody AccountDO accountDO){
        AccountDO accountDb = accountService.getOne(new LambdaQueryWrapper<AccountDO>()
                .eq(AccountDO::getAccountNumber, accountDO.getAccountNumber())
                .ne(AccountDO::getId, accountDO.getId()));
        if(accountDb!=null){
            return Result.success("已存在同 AccountNumber 数据");
        }
        boolean flag = accountService.updateById(accountDO);
        if(flag){
            return Result.success("修改数据成功");
        }else {
            return Result.error();
        }
    }

    @PostMapping("queryList")
    public Result query(@RequestBody AccountDO accountDO){
        LambdaQueryWrapper<AccountDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(accountDO.getAccountNumber()),AccountDO::getAccountNumber, accountDO.getAccountNumber());
        queryWrapper.like(!StringUtils.isEmpty(accountDO.getAccountName()),AccountDO::getAccountName, accountDO.getAccountName());
        List<AccountDO> list = accountService.list(queryWrapper);
        return Result.success(list);
    }


    @PostMapping("queryPage")
    public Result queryPage(@RequestBody AccountDO accountDO){
        IPage page = new Page(1,10);
        LambdaQueryWrapper<AccountDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(accountDO.getAccountNumber()),AccountDO::getAccountNumber, accountDO.getAccountNumber());
        queryWrapper.like(!StringUtils.isEmpty(accountDO.getAccountName()),AccountDO::getAccountName, accountDO.getAccountName());
        IPage pageResult = accountService.page(page, queryWrapper);

        return Result.success(pageResult);
    }


}
